package com.seven.dlandemo.service.manager;

import android.support.annotation.Nullable;
import android.util.Log;

import com.seven.dlandemo.entity.ClingDevice;
import com.seven.dlandemo.ui.MainActivity;

import org.fourthline.cling.model.meta.Device;

import java.util.ArrayList;
import java.util.Collection;

public class ClingDeviceManager {

    private static final String TAG = MainActivity.myTAG + ClingDeviceManager.class.getSimpleName();

    private static ClingDeviceManager INSTANCE = null;

    private Collection<ClingDevice> mDeviceList = null;

    private ClingDeviceManager(){
        mDeviceList = new ArrayList<>();
    }

    public static ClingDeviceManager getInstance() {
        if (INSTANCE == null) {
            synchronized (ClingDeviceManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new ClingDeviceManager();
                }
            }
        }
        return INSTANCE;
    }


    public void removeDevice(ClingDevice device) {
        mDeviceList.remove(device);
    }

    public void addDevice(ClingDevice device) {
        //fixme 这里什么会为null呢
        if (device == null) {
            Log.e(TAG, "device is null");
        }
        if (mDeviceList == null) {
            Log.e(TAG, "mDeviceList is null");
        }
        mDeviceList.add(device);
    }

    public boolean contain(Device device) {
        for (ClingDevice clingDevice : mDeviceList) {
            Device deviceTemp = clingDevice.getDevice();
            if (deviceTemp != null && deviceTemp.equals(device)) {
                return true;
            }
        }
        return false;
    }

    @Nullable
    public ClingDevice getClingDevice(Device device){
        for (ClingDevice clingDevice : mDeviceList){
            Device deviceTemp = clingDevice.getDevice();
            if (deviceTemp != null && deviceTemp.equals(device)){
                return clingDevice;
            }
        }
        return null;
    }

    @Nullable
    public Collection<ClingDevice> getClingDeviceList(){
        return mDeviceList;
    }

    public void setClingDeviceList(Collection<ClingDevice> deviceList) {
        mDeviceList = deviceList;
    }

    public void destroy(){
        mDeviceList = null;
        INSTANCE = null;
    }

}
